package com.ruoyi.framework.config.security.service;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @author cql
 */
@Service
public class PermissionService {

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysRoleService roleService;

    /**
     * 查询用户角色信息
     *
     * @param user
     * @return
     */
    public Set<String> getRolePermission(SysUser user) {

        Set<String> roles = new HashSet<String>();
        /**
         * 顶级管理员拥有所有权限(顶级管理员userId = 1)
         */
        if (user.isAdmin()) {
            roles.add("admin");
        } else {
            roles.addAll(roleService.selectRolePermissionByUserId(user.getUserId()));
        }
        return roles;
    }


    /**
     * 查询用户所有的权限信息（权限字符串）
     *
     * @param user
     * @return
     */
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> perms = new HashSet<String>();
        /**
         * 顶级管理员拥有所有权限(顶级管理员userId = 1)
         */
        if (user.isAdmin()) {
            perms.add("*:*:*");
        } else {
            perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
        }
        return perms;
    }

}
